

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>7.9. Helper functions — MDAnalysis.core.util &mdash; MDAnalysis v0.7.5-devel documentation</title>
    <link rel="stylesheet" href="../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.7.5-devel',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within MDAnalysis v0.7.5-devel documentation"
          href="../../_static/opensearch.xml"/>
    <link rel="shortcut icon" href="../../_static/mdanalysis-logo.ico"/>
    <link rel="top" title="MDAnalysis v0.7.5-devel documentation" href="../../index.html" />
    <link rel="up" title="7. Core modules" href="../core_modules.html" />
    <link rel="next" title="7.10. Setting up logging — MDAnalysis.core.log" href="log.html" />
    <link rel="prev" title="7.8. Constants and unit conversion — MDAnalysis.core.units" href="units.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="log.html" title="7.10. Setting up logging — MDAnalysis.core.log"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="units.html" title="7.8. Constants and unit conversion — MDAnalysis.core.units"
             accesskey="P">previous</a> |</li>
        <li><a href="../../index.html">MDAnalysis v0.7.5-devel documentation</a> &raquo;</li>
          <li><a href="../core_modules.html" accesskey="U">7. Core modules</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/mdanalysis-logo-200x150.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">7.9. Helper functions &#8212; <tt class="docutils literal"><span class="pre">MDAnalysis.core.util</span></tt></a><ul>
<li><a class="reference internal" href="#files-and-directories">7.9.1. Files and directories</a></li>
<li><a class="reference internal" href="#containers-and-lists">7.9.2. Containers and lists</a></li>
<li><a class="reference internal" href="#file-parsing">7.9.3. File parsing</a></li>
<li><a class="reference internal" href="#data-manipulation-and-handling">7.9.4. Data manipulation and handling</a></li>
<li><a class="reference internal" href="#strings">7.9.5. Strings</a></li>
<li><a class="reference internal" href="#mathematics-and-geometry">7.9.6. Mathematics and Geometry</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="units.html"
                        title="previous chapter">7.8. Constants and unit conversion &#8212; <tt class="docutils literal"><span class="pre">MDAnalysis.core.units</span></tt></a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="log.html"
                        title="next chapter">7.10. Setting up logging &#8212; <tt class="docutils literal"><span class="pre">MDAnalysis.core.log</span></tt></a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/documentation_pages/core/util.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <span class="target" id="module-MDAnalysis.core.util"></span><div class="section" id="helper-functions-mdanalysis-core-util">
<h1>7.9. Helper functions &#8212; <a class="reference internal" href="#module-MDAnalysis.core.util" title="MDAnalysis.core.util"><tt class="xref py py-mod docutils literal"><span class="pre">MDAnalysis.core.util</span></tt></a><a class="headerlink" href="#helper-functions-mdanalysis-core-util" title="Permalink to this headline">¶</a></h1>
<p>Small helper functions that don&#8217;t fit anywhere else.</p>
<div class="section" id="files-and-directories">
<h2>7.9.1. Files and directories<a class="headerlink" href="#files-and-directories" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="MDAnalysis.core.util.filename">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">filename</tt><big>(</big><em>name</em>, <em>ext=None</em>, <em>keep=False</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.filename" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new name that has suffix attached; replaces other extensions.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first last docutils">
<dt><em>name</em></dt>
<dd><p class="first last">filename; extension is replaced unless keep=True</p>
</dd>
<dt><em>ext</em></dt>
<dd><p class="first last">extension</p>
</dd>
<dt><em>keep</em></dt>
<dd><p class="first last"><tt class="xref docutils literal"><span class="pre">False</span></tt>: replace existing extension; <tt class="xref docutils literal"><span class="pre">True</span></tt>: keep if exists</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.util.openany">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">openany</tt><big>(</big><em>directory</em><span class="optional">[</span>, <em>mode='r'</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.openany" title="Permalink to this definition">¶</a></dt>
<dd><p>Context manager to open a compressed (bzip2, gzip) or plain file
(uses <a class="reference internal" href="#MDAnalysis.core.util.anyopen" title="MDAnalysis.core.util.anyopen"><tt class="xref py py-func docutils literal"><span class="pre">anyopen()</span></tt></a>).</p>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.util.anyopen">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">anyopen</tt><big>(</big><em>datasource</em>, <em>mode='r'</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.anyopen" title="Permalink to this definition">¶</a></dt>
<dd><p>Open datasource (gzipped, bzipped, uncompressed) and return a stream.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first last docutils">
<dt><em>datasource</em></dt>
<dd><p class="first last">a file or a stream</p>
</dd>
<dt><em>mode</em></dt>
<dd><p class="first last">&#8216;r&#8217; or &#8216;w&#8217;</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.util.greedy_splitext">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">greedy_splitext</tt><big>(</big><em>p</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.greedy_splitext" title="Permalink to this definition">¶</a></dt>
<dd><p>Split extension in path <em>p</em> at the left-most separator.</p>
</dd></dl>

</div>
<div class="section" id="containers-and-lists">
<h2>7.9.2. Containers and lists<a class="headerlink" href="#containers-and-lists" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="MDAnalysis.core.util.iterable">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">iterable</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.iterable" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns <tt class="xref docutils literal"><span class="pre">True</span></tt> if <em>obj</em> can be iterated over and is <em>not</em> a  string.</p>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.util.asiterable">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">asiterable</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.asiterable" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns obj so that it can be iterated over; a string is <em>not</em> treated as iterable</p>
</dd></dl>

</div>
<div class="section" id="file-parsing">
<h2>7.9.3. File parsing<a class="headerlink" href="#file-parsing" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="MDAnalysis.core.util.FORTRANReader">
<em class="property">class </em><tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">FORTRANReader</tt><big>(</big><em>fmt</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.FORTRANReader" title="Permalink to this definition">¶</a></dt>
<dd><p>FORTRANReader provides a method to parse FORTRAN formatted lines in a file.</p>
<p>Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">atomformat</span> <span class="o">=</span> <span class="n">FORTRANReader</span><span class="p">(</span><span class="s">&#39;2I10,2X,A8,2X,A8,3F20.10,2X,A8,2X,A8,F20.10&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;coordinates.crd&#39;</span><span class="p">):</span>
    <span class="n">serial</span><span class="p">,</span><span class="n">TotRes</span><span class="p">,</span><span class="n">resName</span><span class="p">,</span><span class="n">name</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span><span class="p">,</span><span class="n">chainID</span><span class="p">,</span><span class="n">resSeq</span><span class="p">,</span><span class="n">tempFactor</span> <span class="o">=</span> <span class="n">atomformat</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</pre></div>
</div>
<p>Fortran format edit descriptors; see <a class="reference external" href="http://www.webcitation.org/5xbaWMV2x">Fortran Formats</a> for the syntax.</p>
<p>Only simple one-character specifiers supported here: <em>I F E A X</em> (see
<a class="reference internal" href="#MDAnalysis.core.util.FORTRAN_format_regex" title="MDAnalysis.core.util.FORTRAN_format_regex"><tt class="xref py py-data docutils literal"><span class="pre">FORTRAN_format_regex</span></tt></a>).</p>
<p>Strings are stripped of leading and trailing white space.</p>
<p>Set up the reader with the FORTRAN format string.</p>
<p>The string <em>fmt</em> should look like &#8216;2I10,2X,A8,2X,A8,3F20.10,2X,A8,2X,A8,F20.10&#8217;.</p>
<dl class="method">
<dt id="MDAnalysis.core.util.FORTRANReader.number_of_matches">
<tt class="descname">number_of_matches</tt><big>(</big><em>line</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.FORTRANReader.number_of_matches" title="Permalink to this definition">¶</a></dt>
<dd><p>Return how many format entries could be populated with legal values.</p>
</dd></dl>

<dl class="method">
<dt id="MDAnalysis.core.util.FORTRANReader.parse_FORTRAN_format">
<tt class="descname">parse_FORTRAN_format</tt><big>(</big><em>edit_descriptor</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.FORTRANReader.parse_FORTRAN_format" title="Permalink to this definition">¶</a></dt>
<dd><p>Parse the descriptor.</p>
<blockquote>
<div>parse_FORTRAN_format(edit_descriptor) &#8211;&gt; dict</div></blockquote>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Returns :</th><td class="field-body">dict with totallength (in chars), repeat, length,
format, decimals</td>
</tr>
<tr class="field"><th class="field-name">Raises :</th><td class="field-body"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt> if the <em>edit_descriptor</em> is not recognized
and cannot be parsed</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Specifiers: <em>L ES EN T TL TR / r S SP SS BN BZ</em> are <em>not</em>
supported, and neither are the scientific notation <em>Ew.dEe</em>
forms.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="MDAnalysis.core.util.FORTRANReader.read">
<tt class="descname">read</tt><big>(</big><em>line</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.FORTRANReader.read" title="Permalink to this definition">¶</a></dt>
<dd><p>Parse <em>line</em> according to the format string and return list of values.</p>
<p>Values are converted to Python types according to the format specifier.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Returns :</th><td class="field-body">list of entries with appropriate types</td>
</tr>
<tr class="field"><th class="field-name">Raises :</th><td class="field-body"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt> if any of the conversions cannot be made
(e.g. space for an int)</td>
</tr>
</tbody>
</table>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="#MDAnalysis.core.util.FORTRANReader.number_of_matches" title="MDAnalysis.core.util.FORTRANReader.number_of_matches"><tt class="xref py py-meth docutils literal"><span class="pre">FORTRANReader.number_of_matches()</span></tt></a></p>
</div>
</dd></dl>

</dd></dl>

<dl class="data">
<dt id="MDAnalysis.core.util.FORTRAN_format_regex">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">FORTRAN_format_regex</tt><a class="headerlink" href="#MDAnalysis.core.util.FORTRAN_format_regex" title="Permalink to this definition">¶</a></dt>
<dd><p>Regular expresssion (see <a class="reference external" href="http://docs.python.org/library/re.html#module-re" title="(in Python v2.7)"><tt class="xref py py-mod docutils literal"><span class="pre">re</span></tt></a>) to parse a simple <a class="reference external" href="http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html">FORTRAN edit descriptor</a>.
<tt class="docutils literal"><span class="pre">(?P&lt;repeat&gt;\d?)(?P&lt;format&gt;[IFELAX])(?P&lt;numfmt&gt;(?P&lt;length&gt;\d+)(\.(?P&lt;decimals&gt;\d+))?)?</span></tt></p>
</dd></dl>

</div>
<div class="section" id="data-manipulation-and-handling">
<h2>7.9.4. Data manipulation and handling<a class="headerlink" href="#data-manipulation-and-handling" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="MDAnalysis.core.util.fixedwidth_bins">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">fixedwidth_bins</tt><big>(</big><em>delta</em>, <em>xmin</em>, <em>xmax</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.fixedwidth_bins" title="Permalink to this definition">¶</a></dt>
<dd><p>Return bins of width delta that cover xmin,xmax (or a larger range).</p>
<p>dict = fixedwidth_bins(delta,xmin,xmax)</p>
<p>The dict contains &#8216;Nbins&#8217;, &#8216;delta&#8217;, &#8216;min&#8217;, and &#8216;max&#8217;.</p>
</dd></dl>

</div>
<div class="section" id="strings">
<h2>7.9.5. Strings<a class="headerlink" href="#strings" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="MDAnalysis.core.util.convert_aa_code">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">convert_aa_code</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.convert_aa_code" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts between 3-letter and 1-letter amino acid codes.</p>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.util.parse_residue">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">parse_residue</tt><big>(</big><em>residue</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.parse_residue" title="Permalink to this definition">¶</a></dt>
<dd><p>Process residue string.</p>
<dl class="docutils">
<dt>Examples:</dt>
<dd><ul class="first last simple">
<li>&#8220;LYS300:HZ1&#8221; &#8211;&gt; (&#8220;LYS&#8221;, 300, &#8220;HZ1&#8221;)</li>
<li>&#8220;K300:HZ1&#8221; &#8211;&gt; (&#8220;LYS&#8221;, 300, &#8220;HZ1&#8221;)</li>
<li>&#8220;K300&#8221; &#8211;&gt; (&#8220;LYS&#8221;, 300, None)</li>
</ul>
</dd>
</dl>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Argument :</th><td class="field-body">The <em>residue</em> must contain a 1-letter or 3-letter or
4-letter residue string, a number (the resid) and
optionally an atom identifier, which must be separate
from the residue with a colon (&#8221;:&#8221;). White space is
allowed in between.</td>
</tr>
<tr class="field"><th class="field-name">Returns :</th><td class="field-body"><cite>(3-letter aa string, resid, atomname)</cite>; known 1-letter
aa codes are converted to 3-letter codes</td>
</tr>
</tbody>
</table>
</dd></dl>

</div>
<div class="section" id="mathematics-and-geometry">
<h2>7.9.6. Mathematics and Geometry<a class="headerlink" href="#mathematics-and-geometry" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="MDAnalysis.core.util.normal">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">normal</tt><big>(</big><em>vec1</em>, <em>vec2</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.normal" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the unit vector normal to two vectors.</p>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.util.norm">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">norm</tt><big>(</big><em>v</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.norm" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the length of a vector, <tt class="docutils literal"><span class="pre">sqrt(v.v)</span></tt>.</p>
<p>Faster than <a class="reference external" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html#numpy.linalg.norm" title="(in NumPy v2.0.dev-ec56ee1)"><tt class="xref py py-func docutils literal"><span class="pre">numpy.linalg.norm()</span></tt></a> because no frills.</p>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.util.angle">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">angle</tt><big>(</big><em>a</em>, <em>b</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.angle" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the angle between two vectors in radians</p>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.util.stp">
<tt class="descclassname">MDAnalysis.core.util.</tt><tt class="descname">stp</tt><big>(</big><em>vec1</em>, <em>vec2</em>, <em>vec3</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.util.stp" title="Permalink to this definition">¶</a></dt>
<dd><p>Takes the scalar triple product of three vectors.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Returns :</th><td class="field-body">v3 . (v1 x v2)</td>
</tr>
</tbody>
</table>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="log.html" title="7.10. Setting up logging — MDAnalysis.core.log"
             >next</a> |</li>
        <li class="right" >
          <a href="units.html" title="7.8. Constants and unit conversion — MDAnalysis.core.units"
             >previous</a> |</li>
        <li><a href="../../index.html">MDAnalysis v0.7.5-devel documentation</a> &raquo;</li>
          <li><a href="../core_modules.html" >7. Core modules</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2005-2011, Naveen Michaud-Agrawal, Elizabeth J. Denning, Joshua Adelman,
    Christian Beckstein (logo), David Caplan, Jan Domański, Philip Fowler,
    Joseph Goose, Benjamin Hall, Danny Parton, Tyler Reddy, Paul Rigor,
    and Oliver Beckstein.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>
  </body>
</html>